import {createSlice} from "@reduxjs/toolkit";
import {
    getSupplierCountAPI,
    getSupplierCountByNameAPI,
    getSupplierInfoAPI,
    getSupplierListAPI,
    getSupplierListByNameAPI
} from "@/apis/supplier.jsx";


const supplierStore=createSlice({
    name:'supplier',

    initialState:{
        supplierInfo_init:{},
        supplierList_init:[],
        suppliersCount_init:0,
        supplierCur_init:1,
        supplierPageSize_init:11,
        supplierDriveListChange_init:0
    },

//     同步方法
    reducers:{
        getSupplierInfo(state,action){
            state.supplierInfo_init=action.payload
        },

        getSupplierList(state,action){
            state.supplierList_init=action.payload
        },

        getSuppliersCount(state,action){
            state.suppliersCount_init=action.payload
        },
        setSupplierCur(state,action){
            state.supplierCur_init=action.payload;
        },
        setSupplierPagerSize(state,action){
            state.supplierPageSize_init=action.payload;
        },
        // 定义一个状态数据，用于监测选中了那条记录，参数是选中对象中的id,返回值就是这个id值
        setSupplierDriveListChange(state,action){
            state.supplierDriveListChange_init=action.payload;
        }
    }

})

const {setSupplierDriveListChange,getSupplierInfo,getSupplierList,getSuppliersCount,setSupplierCur,setSupplierPagerSize}=supplierStore.actions

// 异步请求函数
const fetchGetSupplierList=(cur,pageSize)=>{
    return async (dispatch)=>{
        const res=await getSupplierListAPI(cur,pageSize);
        dispatch(getSupplierList(res.data))
    }
}

const fetchGetSupplierInfo=(id)=>{
    return async (dispatch)=>{
        const res=await getSupplierInfoAPI(id);
        dispatch(getSupplierInfo(res.data))
    }
}

const fetchGetSuppliersCount=()=>{
    return async (dispatch)=>{
        const res=await getSupplierCountAPI();
        dispatch(getSuppliersCount(res.data))
    }
}

// 根据供应商名称、供应商联系人或者电话号码查询用户（用list，可能有1个或者多个）

const fetchGetSupplierListByName=(name,cur,pageSize)=>{
    return async (dispatch)=>{
        const res=await getSupplierListByNameAPI(name,cur,pageSize);
        dispatch(getSupplierList(res.data))
    }
}

const fetchGetSupplierCountByName=(name)=>{
    return async (dispatch)=>{
        const res=await getSupplierCountByNameAPI(name);
        dispatch(getSuppliersCount(res.data))
    }
}


// 根据时间段查询供应商数据
/*const fetchGetSupplierListByTime=(startTime,endTime,cur,pageSize)=>{
    return async (dispatch)=>{
        const res=await getUserListByTimeAPI(startTime,endTime,cur,pageSize);
        dispatch(getSupplierList(res.data))

    }
}

const fetchGetSupplierCountByTime=(startTime,endTime)=>{
    return async (dispatch)=>{
        const res=await getSupplierCountByTimeAPI(startTime,endTime);
        dispatch(getSuppliersCount(res.data))
    }

}*/


const supplierReducer=supplierStore.reducer;
export {
    fetchGetSupplierList,
    fetchGetSupplierInfo,
    fetchGetSuppliersCount,
    fetchGetSupplierListByName,
    fetchGetSupplierCountByName,
    setSupplierCur,
    setSupplierPagerSize,
    setSupplierDriveListChange

}

export default supplierReducer;




